function add_tag(obj,d=""){
    let temp = tag_cell.cloneNode(true)
    temp.querySelector(".tag_input").value = d
    if(d==review_time[1]){
        temp.querySelector(".tag_input").classList.add("tag_time")
    }

    let par = $(obj).closest(".add_tag")[0]
    par.parentNode.insertBefore(temp, par)
}

function tag_change(obj){
    let par = $(obj).closest(".new")[0]
    let temp = par.querySelectorAll(".tag_input")

    let data_temp = []
    for (let i = 0; i < temp.length; i++){
        data_temp.push(temp[i].value)
    }
    

    let l = par.id

    data_temp = {"id":parseInt(l),"tag_data":data_temp}

    addData(db, store_tag, data_temp)

}

function add_tag_in(id,tag_data){

    let e = document.querySelectorAll(".new")

    for (let i in tag_data){
        try{add_tag(document.getElementById(id).querySelector(".add_tag_button"),tag_data[i])}
        catch(err) {
            
        }
    }


}

 //处理获取的原始数据
 function tag_input(data){
    let temp = data
    for (let i in temp){
      let x = temp[i]
      add_tag_in(x["id"],tag_data=x["tag_data"])
    }
}
 
 //获取所有值
function get_tag(db) {
	  console.log("标签获取")
	  let transaction = db.transaction([store_tag])	
	  let objectStore = transaction.objectStore(store_tag)
      let request = objectStore.getAll()
	  
    request.onerror = function(event) {
        console.log('任务失败')
    }
 
    request.onsuccess = function(event) {
        let temp = request.result
        if(temp.length>0){
        tag_input(temp)
        }
    }
   }

function del_tag(obj){
    let par = $(obj).closest(".tag_cell")[0]
    let temp =par.parentNode
    
    par.remove()
    
    tag_change(temp)
    
}

function add_select(obj,d=""){
    let temp = select_cell.cloneNode(true)
    temp.querySelector(".select_input").value = d

    let par = $(obj).closest(".add_select")[0]
    par.parentNode.insertBefore(temp, par)
}



function tag_serach_input(data_temp,temp){
    for(let i in data_temp){
        //console.log(temp["tag_data"].includes(data_temp[i]),"data_temp[i]:",data_temp[i],"temp:",temp,)
        //window.alert(data_temp)	
        if(temp["tag_data"].includes(data_temp[i])){
            return(temp["id"])
        }
    }
}

// 按标签筛选数据的主函数
function tag_serach(db,store_tag,data_temp){
    let transaction = db.transaction([store_tag])	
    let objectStore = transaction.objectStore(store_tag)
    let request = objectStore.getAll()
    let data_temp_1 = data_temp

    request.onerror = function(event) {
        console.log('任务失败')
    }

    request.onsuccess = function(event) {
        let temp = request.result
        let id_l = []
        for(let i in temp){
            id_t = tag_serach_input(data_temp_1,temp[i])  
            if(id_t>0){
                id_l.push(id_t)
            }
        }
      
        let data_temp_2 = {"re_list":parseInt(1),"result_list":id_l}
    
        addData(db, search_tag, data_temp_2)
    }
}

//按标签筛选数据
function select_data(obj){
    let par = $(obj).parents("#data_select")[0]
    let temp = par.querySelectorAll(".select_input")

    let data_temp = []
    for (let i = 0; i < temp.length; i++){
        data_temp.push(temp[i].value)
    }

    tag_serach(db,store_tag,data_temp)
    location.reload(false)
}

function select_change(obj){
    let par = $(obj).closest("#select_tbody")[0]
    let temp = par.querySelectorAll(".select_input")

    let data_temp = []
    for (let i = 0; i < temp.length; i++){
        data_temp.push(temp[i].value)
    }
    

    data_temp = [{"re_list":0,"search_tab":data_temp},{"re_list":1,"result_list":[]}]

                

    addData(db, search_tag, data_temp[0])
    addData(db, search_tag, data_temp[1])
}

function del_select(obj){
    let par = $(obj).closest(".select_cell")[0]
    let temp =par.parentNode    
    par.remove()
    
    select_change(temp)
}

function get_search(db){
    console.log("数据获取")
    let transaction = db.transaction([search_tag])	
    let objectStore = transaction.objectStore(search_tag)
    let request = objectStore.getAll()

    request.onsuccess = function(event) {
        let temp = request.result
        if(temp.length>0){
            let tag_input = temp[0]["search_tab"]
            obj = document.getElementsByClassName("add_select_button")
            for(let i in tag_input){
                add_select(obj,tag_input[i])
            }
            let tag_list = temp[1]["result_list"]
            get_data(db,tag_list)
            get_tag(db)
        }
    }
    request.onerror = function(event) {
        console.log("未检索到筛选列表")
    }

}